<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org/">

<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
    <title>backend</title>
    <link rel="stylesheet" type="text/css" href="/bootstarp/css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="/css/index.css"/>
    <link rel="stylesheet" type="text/css" href="/css/nmms.css"/>
    <script src="/js/jquery.js" type="text/javascript" charset="utf-8"></script>
    <script src="/bootstarp/js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
    <script src="/js/userSetting.js" type="text/javascript" charset="utf-8"></script>
    <script src="/js/bootstrap-paginator.js" type="text/javascript" charset="utf-8"></script>
    <script>
        // 添加商品类型
        $(function () {

            // 分页
            let options = {
                bootstrapMajorVersion: 3,
                currentPage: "[[${pageInfo.pageNum == 0 ? 1 : pageInfo.pageNum}]]",
                totalPages: "[[${pageInfo.pages == 0 ? 1 : pageInfo.pages}]]",
                size: "normal",
                alignment: "center",
                pageUrl: function (type, page, current) {
                    return "/productType/show?pageNo=" + page;
                }
            }
            $("#page").bootstrapPaginator(options);

            $("#addTo").click(function () {
                $("#error").empty();
                $.ajax({
                    type: 'post',
                    url: '/productType/add',
                    data: {
                        name: $("#productTypeName").val()
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.responseCode == "2001") {
                            location.href = '/productType/show?pageNo=[[${pageInfo.pageNum}]]';
                        } else {
                            $("#error").append("<span>" + res.message + "</span>");
                        }
                    }
                });

            });

            $("input[name='changeState']").click(function () {
                $("#error").empty();
                $.ajax({
                    type: 'post',
                    url: '/productType/modifyStatus',
                    data: {
                        id: $(this).attr("data_id")
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.responseCode == '2001') {
                            location.href = '/productType/show?pageNo=[[${pageInfo.pageNum}]]';
                        } else {
                            $("#error").append("<span>" + res.message + "</span>");
                        }

                    }
                })

            });

            $("input[name='showInfo']").click(function () {
                $("#error").empty();
                $.ajax({
                    type: 'post',
                    url: '/productType/showModify',
                    data: {
                        id: $(this).attr("data_id")
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.responseCode == '2001') {
                            $("#proTypeNum").val(res.returnObject.id)
                            $("#proTypeName").val(res.returnObject.name)
                        } else {
                            $("#error").append("<span>" + res.message + "</span>");
                        }

                    }
                })
            })

            $("#modify").click(function () {
                $("#error").empty();
                $.ajax({
                    type: 'post',
                    url: '/productType/modify',
                    data: {
                        id: $("#proTypeNum").val(),
                        name: $("#proTypeName").val()
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.responseCode == '2001') {
                            location.href = '/productType/show?pageNo=[[${pageInfo.pageNum}]]';
                        } else {
                            $("#error").append("<span>" + res.message + "</span>");
                        }

                    }
                })
            })


        });

    </script>
</head>

<body>
<div class="panel panel-default" id="userSet">
    <div class="panel-heading">
        <h3 class="panel-title">商品类型管理<span id="error" style="color: red"></span></h3>
    </div>
    <div class="panel-body">
        <input type="button" value="添加商品类型" class="btn btn-primary" id="doAddProTpye">
        <div class="modal fade" tabindex="-1" id="ProductType">
            <!-- 窗口声明 -->
            <div class="modal-dialog modal-lg">
                <!-- 内容声明 -->
                <div class="modal-content">
                    <!-- 头部、主体、脚注 -->
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">添加商品类型信息</h4>
                    </div>
                    <div class="modal-body text-center">
                        <div class="row text-right">
                            <label for="productTypeName" class="col-sm-4 control-label">类型名称：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="productTypeName">
                            </div>
                        </div>
                        <br>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary addProductType" id="addTo">添加</button>
                        <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>
        <br>
        <br>
        <div class="show-list">
            <table class="table table-bordered table-hover" style='text-align: center;'>
                <thead>
                <tr class="text-danger">
                    <th class="text-center">编号</th>
                    <th class="text-center">类型名称</th>
                    <th class="text-center">状态</th>
                    <th class="text-center">操作</th>
                </tr>
                </thead>
                <tbody id="tb">
                    <tr th:each="type:${productTypes}">
                        <td>[[${type.id}]]</td>
                        <td>[[${type.name}]]</td>
                        <td>
                            <span th:if="${type.status == -1}">禁用</span>
                            <span th:if="${type.status == 1}">启用</span>
                        </td>
                        <td class="text-center">
                            <input type="button" class="btn btn-warning btn-sm doProTypeModify" name="showInfo"
                                   th:data_id="${type.id}" value="修改">
                            <span th:if="${type.status == -1}">
                                <input type="button" class="btn btn-danger btn-sm doProTypeDisable" th:data_id="${type.id}"
                                       name="changeState" value="禁用"/>
                            </span>
                            <span th:if="${type.status == 1}">
                                <input type="button" class="btn btn-success btn-sm doProDisable" th:data_id="${type.id}"
                                       name="changeState" value="启用"/>
                            </span>
                        </td>
                    </tr>
                </tbody>
            </table>
            <ul id="page"></ul>
        </div>
        <div class="modal fade" tabindex="-1" id="myProductType">
            <!-- 窗口声明 -->
            <div class="modal-dialog modal-lg">
                <!-- 内容声明 -->
                <div class="modal-content">
                    <!-- 头部、主体、脚注 -->
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">商品修改</h4>
                    </div>
                    <div class="modal-body text-center">
                        <div class="row text-right">
                            <label for="proTypeNum" class="col-sm-4 control-label">编号：</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="proTypeNum" readonly>
                            </div>
                        </div>
                        <br>
                        <div class="row text-right">
                            <label for="proTypeName" class="col-sm-4 control-label">类型名称</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" id="proTypeName">
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-warning updateProType" id="modify">修改</button>
                        <button class="btn btn-primary cancel" data-dismiss="modal">取消</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

</html>